<?php
declare(strict_types=1);

namespace app\admin\controller;

use app\admin\service\UsersMemberService;
use app\AdminController;

/**
 * 会员
 * Class Member
 * @package app\admin\controller
 */
class Member extends AdminController
{
    /**
     * 会员列表
     * @return \think\response\Json|\think\response\View
     */
    public function index()
    {
        if (request()->isPost()) {
            $where = [];
            // 关键词搜索
            $keywords = $this->request->param('keywords', '');
            if ($keywords) {
                $where[] = ['u.nickname', 'like', '%' . $keywords . '%'];
            }

            $list = UsersMemberService::getUsersMemberList($where, $this->page, $this->pagesize);
            $count = UsersMemberService::countUsersMemberList($where);
            return adminResponseJson(['list' => $list, 'count' => $count]);
        }

        // 本月会员用户（折线图）
        $usersMemberMonthLine = UsersMemberService::getUsersMemberMonthLine();
        $monthWhere = ['mid' => 3];
        $todayWhere = ['mid' => 2];
        $yearsWhere = ['mid' => 1];
        // 本周月会员总数
        $monthUsersMemberThisWeek = UsersMemberService::countUsersMemberWeek($monthWhere);
        // 本月月会员总数
        $monthUsersMemberThisMonth = UsersMemberService::countUsersMemberMonth($monthWhere);
        // 本周年会员总数
        $yearsUsersMemberThisWeek = UsersMemberService::countUsersMemberWeek($yearsWhere);
        // 本月年会员总数
        $yearsUsersMemberThisMonth = UsersMemberService::countUsersMemberMonth($yearsWhere);
        // 本周日会员总数
        $todayUsersMemberThisWeek = UsersMemberService::countUsersMemberWeek($todayWhere);
        // 本月日会员总数
        $todayUsersMemberThisMonth = UsersMemberService::countUsersMemberMonth($todayWhere);
//        var_dump($usersMemberMonthLine);exit;
        // 会员统计
        $countUsersMember = UsersMemberService::countUsersMember();
        // 失效会员统计
        $countInvalidUsersMember = UsersMemberService::countInvalidUsersMember();
        // 年会员
        $yearsUsersMember = UsersMemberService::countUsersMember($yearsWhere);
        // 月会员
        $monthUsersMember = UsersMemberService::countUsersMember($monthWhere);

        return view('', [
            'member_data' => [
                'usersMemberMonthLine' => $usersMemberMonthLine,
                'monthUsersMemberThisWeek' => $monthUsersMemberThisWeek,
                'monthUsersMemberThisMonth' => $monthUsersMemberThisMonth,
                'yearsUsersMemberThisWeek' => $yearsUsersMemberThisWeek,
                'yearsUsersMemberThisMonth' => $yearsUsersMemberThisMonth,
                'todayUsersMemberThisWeek' => $todayUsersMemberThisWeek,
                'todayUsersMemberThisMonth' => $todayUsersMemberThisMonth,
            ],
            'open_data' => [
                'countUsersMember' => $countUsersMember,
                'countInvalidUsersMember' => $countInvalidUsersMember,
                'yearsUsersMember' => $yearsUsersMember,
                'lastYearsUsersMember' => $countUsersMember - $yearsUsersMember,
                'yearsUsersMemberScale' => round(($yearsUsersMember / $countUsersMember) * 100, 2) . '%',
                'monthUsersMember' => $monthUsersMember,
                'lastMonthUsersMember' => $countUsersMember - $monthUsersMember,
                'monthUsersMemberScale' => round(($monthUsersMember / $countUsersMember) * 100, 2) . '%',
            ]
        ]);
    }
}